/**
 * Created by zoulu on 15/10/14.
 */

var Manage = {
	init: function() {
		var url = location.search; //获取url中"?"符后的字串
		var theRequest = "";
		if (url.indexOf("?") != -1) {
			theRequest = url.split("=")[1];
		}
		Manage.get_data(0, 10, theRequest);
		$(".pagination li a").click(function() {
			var page = $(this).attr("page");
			var size = 10;
			Manage.get_data(page, size, theRequest);
			Common.init_pagination(page);
		});
		$("a[name='add_photo']").click(Manage.add_teacher);
		$("#data_table thead").find("input[name='job_id']").attr("value", theRequest)
	},
	add_teacher: function() {
		var job_id = $("#data_table thead").find("input[name='job_id']").val();
		var obj = document.getElementsByName("checkbox");
		var user_id = "";
		for (var i = 0; i < obj.length; i++) {
			if (obj[i].checked) {
				user_id = obj[i].value;
			}
		}
	
		ajax("/jobs/select-teacher", {
			"job_id": job_id,
			"user_id": user_id
			
		}, success());
	},
	get_data: function(page, size, theRequest) {
		if (page == undefined) {
			page = 0;
		}
		if (size == undefined) {
			size = 10;
		}
		ajax("/jobs/candidates/list", {
			"page": page,
			"size": size,
			"job_id": theRequest
		}, callback_init_table);
	},

	init_table: function(result) {
		if (result != null && result.data != null && result.data.candidates.length > 0) {
			var tbody = $("#data_table tbody");
			tbody.empty();
			var candidates = result.data.candidates;
			var trs = "";
			for (var i = 0; i < candidates.length; i++) {
				var candidate = candidates[i];
				var user = candidate.user;

				trs += ("<tr>");
				var status = "";
				if (user.status == 1) {
					status = "待审核认证";
				}
				if (user.status == 0) {
					status = "正常";
				}

				var gender = "未知";
				if (user.gender == 1) {
					gender = "女性";
				}
				if (user.gender == 2) {
					gender = "男性";
				}
				if (user.gender == 0) {
					gender = "未知";
				}
				var option = "<td>";
				if (user.status == 0) {
					option += ("<input type=\"radio\" name='checkbox' value=\"" + user.id + "\" />");
				} else {
					option += ("<input type=\"radio\"  disabled='disabled'' name='checkbox' value=\"" + user.id + "\" />");
				}
				option += "</td>";
				var name = ("<td>" + user.name + "</td>");

				var nick = ("<td>" + user.nick + "</td>");
				var gender_str = ("<td>" + gender + "</td>");
				var birthday = ("<td>" + formatDate(user.birthday) + "</td>");
				var created_at = ("<td>" + formatDate(user.created_at) + "</td>");
				var status_str = ("<td>" + status + "</td>");

				trs += (option + name + nick + gender_str + birthday + created_at + status_str);
				trs += ("</tr>");
			}
			tbody.append(trs);
		}
		else{
			$("#select_teacher").hide();
		}
	}
};

function success(result) {
	
	if (result != null && result.code == 0) {
		window.location.hre="./jobs_list.html";
	} else {
		alert("操作失败，请刷新页面重试！");
	}
}

function callback_init_table(result) {
	Manage.init_table(result);
}

function callback_init_class(result) {
	Manage.init_class(result);
}

$(function() {
	Manage.init();
})